package com.cunjin.base.service.impl;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.cunjin.base.common.BaseSystemContent;
import com.cunjin.base.dao.BaseDao;
import com.cunjin.base.service.BaseService;


public class BaseServiceImpl<T> implements BaseService<T> {
	private String succeesCoce = BaseSystemContent.SUCCESS_CODE;
	private String failureCoce = BaseSystemContent.FAILURE_CODE;

	@Autowired
	private BaseDao<T> dao;

	/**
	 * readOnly=false：增/删/改 readOnly=true ：查询
	 * 
	 * 考虑重复性
	 */
	@Transactional(readOnly = false)
	@Override
	public Map<String, Object> insert(T entity) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			//先判断是否重复
			int count = dao.insert(entity);
			map.put("code", succeesCoce);
			map.put("count", count);
		} catch (Exception e) {
			map.put("code", failureCoce);
			e.printStackTrace();
		}
		return map;
	}

	
	/**
	 * code:成功码
	 * 
	 */
	@Override
	public Map<String, Object> deleteByid(T entity) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Field idField = entity.getClass().getDeclaredField("id");
			idField.setAccessible(true);
			Object id = idField.get(entity);
			int count = dao.deleteByPrimaryKey(id);
			map.put("code", succeesCoce);
			map.put("count", count);
		} catch (Exception e) {
			map.put("code", failureCoce);
			e.printStackTrace();
		}
		return map;
	}

	@Override
	public Map<String, Object> update(T entity) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			int count=dao.updateByPrimaryKeySelective(entity);
			map.put("code", succeesCoce);
			map.put("count", count);
		} catch (Exception e) {
			map.put("code", failureCoce);
			e.printStackTrace();
		}
		return map;
	}

	@Override
	public Map<String, Object> queryById(T entity) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Field idField = entity.getClass().getDeclaredField("id");
			idField.setAccessible(true);
			Object id = idField.get(entity);
			T queryById = dao.selectByPrimaryKey(id);
			map.put("code", succeesCoce);
			map.put("entity", queryById);
		} catch (Exception e) {
			map.put("code", failureCoce);
			e.printStackTrace();
		}
		return map;
	}

	@Override
	public Map<String, Object> queryAll() {
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			List<T> list = dao.selectAll();
			map.put("code", succeesCoce);
			map.put("list", list);
		} catch (Exception e) {
			map.put("code", failureCoce);
			e.printStackTrace();
		}
		return map;
	}

	
	/**
	 * pageIndex:当前的页数
	 * listCount：一共有几条数据
	 * list：当前的分页列表
	 * yeshu：分几页
	 */
	@Override
	public Map<String, Object> queryByPage(Integer pageIndex, Integer pageSize) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			int offset = (pageIndex - 1) * pageSize;

			List<T> listByPage = dao.selectByExampleAndRowBounds(null, new RowBounds(offset, pageSize));
			//获得分页的总数目
			 int acount=dao.selectAll().size();
			//如果不能整除，页数加一，否则等于页数除以分页的数
			int yeshu=acount % pageSize==0?acount/pageSize:acount/pageSize+1;
			 
			map.put("total", acount);
			map.put("rows", listByPage);
			map.put("yeshu", yeshu);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	
	}
}
